// pages/prePublish/prePublish.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid:'',
        noticeList:'',
        noticeList1:'',
        noticeList2:'',
        i:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad:function(){

        this.getNoticeList()
    },
    onShow: function (options) {
        const openid = wx.getStorageSync('openid');
          this.setData({
              openid
          })
    },
    toPublishSecond(){
        wx.navigateTo({
            url: '/pages/publish/publish',
        });
          
    },
    toPublishAsk(){
        wx.navigateTo({
            url: '/pages/ask/ask',
        });
    },
    toPublishFree(){
        wx.navigateTo({
            url: '/pages/free/free',
        });
    },


    // 获取公告列表
    getNoticeList(){
        wx.request({
            url: 'http://127.0.0.1:6060/api/getnoticeList',
            header: {'content-type':'application/json'},
            method: 'get',
           
            success: (result) => {
                this.setData({
                    noticeList:result.data.data
                })
                const noticeList1 = result.data.data.filter((item)=>{
                    return item.state == 'true' && item.type == '发布说明'
                })
                const noticeList2 = result.data.data.filter((item)=>{
                    return item.state == 'true' && item.type == '注意事项'
                })
                this.setData({
                    noticeList1,
                    noticeList2
                })
            },
           
        });
    },

    
    login(e){
       
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log(res.userInfo)
              this.setData({
                userInfo: res.userInfo,
                hasUserInfo: true
              })
              wx.login({
                //获取code
               
                success:  (res)=> {
                  var code = res.code; //返回code
                  const userInfo = this.data.userInfo
                  wx.setStorageSync("userInfo",userInfo);
                  wx.request({
                      url: 'http://127.0.0.1:6060/api/addUserInfo',
                      data: {
                          code,
                          userInfo
                        
                      },
                      header: {'content-type':'application/json'},
                      method: 'post',
                     
                      success: (result) => {
                          console.log(result.data.message)
                          this.setData({
                              openid:result.data.message
                          })
                          wx.setStorageSync("openid",result.data.message);
                      },
                     
                  });
                    
                }
            })
              wx.showToast({
                  title: '登录成功',
                  icon: 'success',
                 
                  duration: 1500,
                  mask: false,
                  success: (result) => {
                      
                  },
                  fail: () => {},
                  complete: () => {}
              });
                
           
            }
          })
        }

   
})